package com.len.utils;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by songxiangyang on 2018/3/31.
 */
public class DateUtil {


    /**
     * 获取同比天数
     * @param timeStr
     * @return
     */
    public static String[] dateCompareTransformDays(String timeStr) {
        String startTime = "";
        String endTime = "";
        String[] dtArr = new String[2];
        if(StringUtils.isBlank(timeStr)){
            DateTime now = new DateTime();
            DateTime yesterday = now.plusDays(-8);
            DateTime lastSeven = now.plusDays(-14);
            String endTimeString = yesterday.toString("yyyy-MM-dd");
            String startTimeString = lastSeven.toString("yyyy-MM-dd");
            startTime = startTimeString;
            endTime = endTimeString;
            dtArr[0] = startTime;
            dtArr[1] = endTime;
            return dtArr;
        }
        String[] splitTime = timeStr.split(" - ");

        if(splitTime.length == 0){
            DateTime now = new DateTime();
            DateTime yesterday = now.plusDays(-7);
            DateTime lastSeven = now.plusDays(-14);
            String endTimeString = yesterday.toString("yyyy-MM-dd");
            String startTimeString = lastSeven.toString("yyyy-MM-dd");
            startTime = startTimeString;
            endTime = endTimeString;

        }
        else if(splitTime.length>1) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date start = null;
            Date end = null;
            try {
                start = sdf.parse(splitTime[0]);
                end = sdf.parse(splitTime[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            DateTime dt1 = new DateTime(start);
            DateTime dt2 = new DateTime(end);
            int intervalDays = Days.daysBetween(dt1, dt2).getDays();
            DateTime startDate = dt1.plusDays(-(intervalDays+1));
            DateTime endDate = dt1.plusDays(-1);
            startTime = startDate.toString("yyyy-MM-dd");
            endTime = endDate.toString("yyyy-MM-dd");
        }
        dtArr[0] = startTime;
        dtArr[1] = endTime;
        return dtArr;
    }

    /**
     * 将日期字符串转换成两个日期
     * @param timeStr
     * @return
     */
    public static String[] dateTransformOrLastSevenDays(String timeStr){
        String startTime = "";
        String endTime = "";
        String[] dtArr = new String[2];
        if(StringUtils.isBlank(timeStr)){
            DateTime now = new DateTime();
            DateTime yesterday = now.plusDays(-1);
            DateTime lastSeven = now.plusDays(-7);
            String endTimeString = yesterday.toString("yyyy-MM-dd");
            String startTimeString = lastSeven.toString("yyyy-MM-dd");
            startTime = startTimeString;
            endTime = endTimeString;
            dtArr[0] = startTime;
            dtArr[1] = endTime;
            return dtArr;
        }
        String[] splitTime = timeStr.split(" - ");

        if(splitTime.length == 0){
            DateTime now = new DateTime();
            DateTime yesterday = now.plusDays(-1);
            DateTime lastSeven = now.plusDays(-7);
            String endTimeString = yesterday.toString("yyyy-MM-dd");
            String startTimeString = lastSeven.toString("yyyy-MM-dd");
            startTime = startTimeString;
            endTime = endTimeString;

        }
        else if(splitTime.length>1) {
            startTime = splitTime[0];
            endTime = splitTime[1];
        }
        dtArr[0] = startTime;
        dtArr[1] = endTime;
        return dtArr;
    }

    /**
     * 将日期字符串转换成两个日期
     * @param timeStr
     * @return
     */
    public static String[] dateTransform(String timeStr){
        String startTime = "";
        String endTime = "";
        String[] dtArr = new String[2];
        if(StringUtils.isBlank(timeStr)){
            DateTime now = new DateTime();
            DateTime yesterday = now.plusDays(-1);
            String timeString = yesterday.toString("yyyy-MM-dd");
            startTime = timeString;
            endTime = timeString;
            dtArr[0] = startTime;
            dtArr[1] = endTime;
            return dtArr;
        }
        String[] splitTime = timeStr.split(" - ");

        if(splitTime.length == 0){
            DateTime now = new DateTime();
            DateTime yesterday = now.plusDays(-1);
            String timeString = yesterday.toString("yyyy-MM-dd");
            startTime = timeString;
            endTime = timeString;

        }
        else if(splitTime.length>1) {
            startTime = splitTime[0];
            endTime = splitTime[1];
        }
        dtArr[0] = startTime;
        dtArr[1] = endTime;
        return dtArr;
    }

    public static void main(String[] args) {
//        String[] strings = dateCompareTransformDays("2018-05-29 - 2018-06-04");
//        System.out.println(strings[0] + "     " + strings[1]);
//
//        String[] strings1 = dateTransformOrLastSevenDays("2018-05-29 - 2018-06-04");
//        System.out.println(strings1[0] + "     " + strings1[1]);
        BigDecimal a1 = new BigDecimal(10.0 + "").setScale(7, BigDecimal.ROUND_HALF_UP);
        BigDecimal a2 = new BigDecimal(6.3 + "").setScale(7, BigDecimal.ROUND_HALF_UP);

        BigDecimal r = (a2.subtract(a1)).divide(a1, 4, BigDecimal.ROUND_HALF_EVEN).setScale(4,
                BigDecimal.ROUND_HALF_UP);

        NumberFormat percent = NumberFormat.getPercentInstance();
        percent.setMaximumFractionDigits(2);

        System.out.println(percent.format(r.doubleValue()));
    }
}



